package com.dushibao.dao;

import com.dushibao.entity.Resource;
import com.dushibao.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDateTime;

public class ResourceDao {

    public int save(Resource resource){
        try{
            resource.setAddTime(LocalDateTime.now());
            String sql = "insert into dushibao_resource(path) values(?)";
            Connection conn = DBUtils.getConnection();
            PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            stmt.setString(1,resource.getPath());
            stmt.executeUpdate();
            ResultSet rs = stmt.getGeneratedKeys();
            if(rs.next()){
                Long id = rs.getLong(1);
                resource.setId(id);
            }
            DBUtils.close(conn);
            return 1000;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

}
